package com.meepo.sdk.utils.contact

import android.net.Uri
import android.provider.ContactsContract
import com.meepo.sdk.utils.Utils

class ContactUtils {
    companion object {

        @JvmStatic
        fun getContactsByUri(uri: Uri): ArrayList<ContactBrief> {
            val rsp = ArrayList<ContactBrief>()

            Utils.getApp().contentResolver.query(uri, null, null, null, "")?.let { cursor ->

                if (cursor.moveToFirst()) {
                    do {
                        var contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))

                        val cb = ContactBrief(
                            id = contactId,
                            name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
                            phone = "",
                            note = ""
                        )

                        //获取联系人电话号码
                        Utils.getApp().contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null
                        )?.let { phoneCursor ->
                            while (phoneCursor.moveToNext()) {
                                var phone: String =
                                    phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                                phone = phone.replace("-", "")
                                phone = phone.replace(" ", "")
                                cb.phone = phone
                            }

                            phoneCursor.close()
                        }

                        //获取联系人备注信息
                        Utils.getApp().contentResolver.query(
                            ContactsContract.Data.CONTENT_URI,
                            arrayOf(ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Nickname.NAME),
                            ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "='"
                                    + ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE + "'",
                            arrayOf(contactId),
                            null
                        )?.let { noteCursor ->
                            while (noteCursor.moveToNext()) {
                                var note =
                                    noteCursor.getString(noteCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME))
                                cb.note = note
                            }

                            noteCursor.close()
                        }

                        rsp.add(cb)

                    } while (cursor.moveToNext())
                }

                cursor.close()
            }
            return rsp
        }

        @JvmStatic
        fun getContacts(page: Int, size: Int): List<ContactBrief> {

            val rsp = ArrayList<ContactBrief>()

            val uri: Uri = ContactsContract.Contacts.CONTENT_URI

            Utils.getApp().contentResolver.query(uri, null, null, null, "")?.let { cursor ->

                if (cursor.moveToPosition(page * size)) {
                    do {
                        var contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))

                        val cb = ContactBrief(
                            id = contactId,
                            name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)),
                            phone = "",
                            note = ""
                        )

                        //获取联系人电话号码
                        Utils.getApp().contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null
                        )?.let { phoneCursor ->
                            while (phoneCursor.moveToNext()) {
                                var phone: String =
                                    phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                                phone = phone.replace("-", "")
                                phone = phone.replace(" ", "")
                                cb.phone = phone
                            }

                            phoneCursor.close()
                        }

                        //获取联系人备注信息
                        Utils.getApp().contentResolver.query(
                            ContactsContract.Data.CONTENT_URI,
                            arrayOf(ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Nickname.NAME),
                            ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "='"
                                    + ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE + "'",
                            arrayOf(contactId),
                            null
                        )?.let { noteCursor ->
                            while (noteCursor.moveToNext()) {
                                var note =
                                    noteCursor.getString(noteCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME))
                                cb.note = note
                            }

                            noteCursor.close()
                        }

                        rsp.add(cb)

                        if (rsp.size >= size) {
                            break
                        }
                    } while (cursor.moveToNext())
                }

                cursor.close()
            }
            return rsp
        }

    }
}